<?php
// 只能在当前 request 生命周期缓存，要跨进程，可以再加一层缓存： memcached/xcache/apc/
$g_static_city = array(); // 变量缓存

// hook model_user_start.php

// ------------> 最原生的 CURD，无关联其他数据。

function city__create($arr) {
	// hook model_city__create_start.php
	$r = db_insert('city', $arr);
	// hook model_city__create_end.php
	return $r;
}

function city__update($cid, $update) {
	// hook model_city__update_start.php
	$r = db_update('city', array('cid' => $cid), $update);
	// hook model_city__update_end.php
	return $r;
}

function city__read($cid) {
	// hook model_city__read_start.php
	$city = db_find_one('city', array('cid' => $cid));
	// hook model_city__read_end.php
	return $city;
}

function city__delete($cid) {
	// hook model_city__delete_start.php
	$r = db_delete('city', array('cid' => $cid));
	// hook model_city__delete_end.php
	return $r;
}

function city__find($cond = array(), $orderby = array(), $page = 1, $pagesize = 1000) {
	// hook model_city__find_start.php
	$citylist = db_find('city', $cond, $orderby, $page, $pagesize, 'cid');
	// hook model_city__find_end.php
	return $citylist;
}
// ------------> 关联 CURD，主要是强相关的数据，比如缓存。弱相关的大量数据需要另外处理。

// 新建城市
function city_create($name) {
	$r = city__create(array(
		'name' => $name,
	));
	return $r;
}

// 更新城市
function city_update($cid, $update) {
	$r = city__update($cid, $update);

	return $r;
}

// 查看单个城市
function city_read($cid) {
	$city = city__read($cid);
	return $city;
}

function city_read_cache($cid) {
	global $conf;
	$city = cache_get('city-' . $cid);

	// hook model_forum_list_cache_start.php

	if ($city === NULL) {
		$city = city_read($cid);
		cache_set('city-' . $cid, $city, 60);
	}
	// hook model_forum_list_cache_end.php
	return $city;
};

// 查看所有城市
function city_read_all() {
	$citylist = city__find();
	return $citylist;
}

function city_read_all_cache() {
	global $conf, $citylist;
	$citylist = cache_get('citylist');

	// hook model_forum_list_cache_start.php

	if ($citylist === NULL) {
		$citylist = city_read_all();
		cache_set('citylist', $citylist, 60);
	}
	// hook model_forum_list_cache_end.php
	return $citylist;
};

// 查看所有省
function province_read_all() {
	$citylist = city__find();
	$plist = arrlist_key_values($citylist, 'cid', 'province');
	$plist = array_unique($plist);
	return $plist;
}

function province_read_all_cache() {
	global $conf, $plist;
	$plist = cache_get('plist');

	// hook model_forum_list_cache_start.php

	if ($plist === NULL) {
		$plist = province_read_all();
		cache_set('plist', $plist, 60);
	}
	// hook model_forum_list_cache_end.php
	return $plist;
};

?>